package cn.net.lwj.utils;

import javax.servlet.ServletContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class SpringBeanHelper {
	private static WebApplicationContext wcx = null;

	/**
	 * 获取SPRING BEAN对象
	 * 
	 * @param sc
	 * @param beanId
	 * @return
	 */
	public static Object getBean(ServletContext sc, String beanId) {
		initWebContext(sc);
		return getBean(beanId);
	}

	/**
	 * 调用此方法前，必须调用过 initWebContext方法。
	 * 获取SPRING BEAN 对象
	 * 
	 * @param beanId
	 * @return
	 */
	public static Object getBean(String beanId) {
		return wcx.getBean(beanId);
	}

	/**
	 * 初始化 WebApplicationContext 容器
	 * 
	 * @param
	 */
	public static void initWebContext(ServletContext sc) {
		if (wcx == null) {
			wcx = WebApplicationContextUtils.getWebApplicationContext(sc);
		}
	}
}
